﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Common.Utils.Helper
{
    public static class UnityHelper
    {
        /// <summary>
        ///     查找子对象
        /// </summary>
        /// <param name="goParent">父对象</param>
        /// <param name="childName">子对象名称</param>
        /// <returns></returns>
        public static Transform FindTheChild(GameObject goParent, string childName)
        {
            Transform searchTrans = goParent.transform.Find(childName);
            if (searchTrans == null)
                foreach (Transform trans in goParent.transform)
                {
                    searchTrans = FindTheChild(trans.gameObject, childName);
                    if (searchTrans != null)
                        return searchTrans;
                }
            return searchTrans;
        }


        /// <summary>
        ///     获取子物体的组件
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="goParent">父对象</param>
        /// <param name="childName">子对象名称</param>
        /// <returns></returns>
        public static T GetTheChildComponent<T>(GameObject goParent, string childName) where T : Component
        {
            Transform searchTrans = FindTheChild(goParent, childName);
            if (searchTrans != null)
                return searchTrans.gameObject.GetComponent<T>();
            return null;
        }


        /// <summary>
        ///     给子物体添加组件
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="goParent">父对象</param>
        /// <param name="childName">子对象名称</param>
        /// <returns></returns>
        public static T AddTheChildComponent<T>(GameObject goParent, string childName) where T : Component
        {
            Transform searchTrans = FindTheChild(goParent, childName);
            if (searchTrans != null)
            {
                T[] theComponentsArr = searchTrans.GetComponents<T>();
                for (int i = 0; i < theComponentsArr.Length; i++)
                    if (theComponentsArr[i] != null)
                        Object.Destroy(theComponentsArr[i]);
                return searchTrans.gameObject.AddComponent<T>();
            }
            return null;
        }


        /// <summary>
        ///     给子物体添加父对象
        /// </summary>
        /// <param name="parentTrs">父对象的方位</param>
        /// <param name="childTrs">子对象的方位</param>
        public static void AddChildToParent(Transform parentTrs, Transform childTrs)
        {
            //childTrs.parent = parentTrs; //Original Method
            childTrs.SetParent(parentTrs, false);
            childTrs.localPosition = Vector3.zero;
            childTrs.localScale = Vector3.one;
            childTrs.localEulerAngles = Vector3.zero;
        }
    }
}

